﻿using BaiTu.DAL.EF;
using BaiTu.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using BaiTu.IOCServices;
using System.Diagnostics;

namespace BaiTu.Data.Testing
{
    class program
    {

        static void Main(string[] args)
        {
            EngineContext.Initialize(false);
            DAL.UserDAL userDAL = new DAL.UserDAL();
            List<Domains.UserEntity> listEntity = new List<Domains.UserEntity>();

            for (int i = 0; i <= 100; i++)
            {
                Domains.UserEntity userEntity = new Domains.UserEntity()
                {
                    Age = 20,
                    CreateTime = DateTime.Now,
                    Description = "描述信息",
                    IsDelete = false,
                    ModifyTime = DateTime.Now,
                    Name = "郭志奇"+i
                };
                listEntity.Add(userEntity);
            }
            using (userDAL)
            {
                var result = userDAL.Add(listEntity, true);
            }
            userDAL.Commit();
            var list = userDAL.Query(t => !string.IsNullOrEmpty(t.ID));
            //Console.WriteLine(innerResult.Success+"  "+innerResult.ErrorMessage);
            Console.WriteLine("complete"+list.Count());
            Console.Read();
        }


    }

    public class MM
    {
        private ITest _test;
        public MM(ITest test)
        {
            _test = test;
        }
        public void Show()
        {

        }
    }

    public interface ITest
    {
        void Show();
    }
    public class Dependency : BaiTu.IOCServices.IDependencyRegistrater
    {

        public void Register(Autofac.ContainerBuilder builder, BaiTu.Framework.Core.TypeFinder.ITypeFinder typeFinder)
        {
            builder.RegisterType<Test>().As<ITest>().InstancePerDependency();
        }

        public int Order
        {
            get { return 1000; }
        }
    }
    public class Test : ITest
    {
        public void Show()
        {
            //var ss = TableNoTracking.ToList();
            //foreach (var item in ss)
            //{
            //    Delete(item);
            //}
            //ss = TableNoTracking.ToList();
            //Console.WriteLine(ss.Count());
            DAL.UserDAL userDal = new DAL.UserDAL();
            userDal.Add(new Domains.UserEntity()
            {
                Age = 1000,
                CreateTime = DateTime.Now,
                IsDelete = false
            });

            List<Domains.UserEntity> list = new List<Domains.UserEntity>();
            for (int i = 0; i <= 10; i++)
            {
                var ss = new Domains.UserEntity()
                   {
                       Age = i,
                       CreateTime = DateTime.Now,
                       IsDelete = false
                   };
                list.Add(ss);
                //  Add(ss);
            }
            Add(list);
            Console.WriteLine(Table.Count());
        }
        protected IDBContext _context;
        private IDataProvider _dataProvider;
        private IRepository<Domains.UserEntity> _repository;
        public Test(IDBContext context, IDataProvider dataProvider, IRepository<Domains.UserEntity> repository)
        {
            this._context = context;
            _dataProvider = dataProvider;
            _repository = repository;
        }


        public Framework.InnerResult Add(IEnumerable<Domains.UserEntity> list)
        {

            return _repository.Add(list);
        }

        public Framework.InnerResult Delete(IEnumerable<Domains.UserEntity> list)
        {
            return _repository.Delete(list);
        }

        public Framework.InnerResult Update(IEnumerable<Domains.UserEntity> list)
        {
            return _repository.Update(list);
        }

        public IEnumerable<Domains.UserEntity> Query(Func<Domains.UserEntity, bool> predicate)
        {
            return _repository.Query(predicate);
        }

        public IList<TEntity> ExecuteStoredProcedureList<TEntity>(string commandText, params object[] parameters) where TEntity : Domains.BaseDomain, new()
        {
            return _repository.ExecuteStoredProcedureList<TEntity>(commandText);
        }

        public IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters)
        {
            return _repository.SqlQuery<TElement>(sql);
        }

        public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, int? timeout = null, params object[] parameters)
        {
            return _repository.ExecuteSqlCommand(sql, true);
        }

        public Domains.UserEntity GetById(string id)
        {
            throw new NotImplementedException();
        }

        public Framework.InnerResult Add(Domains.UserEntity t)
        {
            return _repository.Add(t);
        }

        public Framework.InnerResult Delete(Domains.UserEntity t)
        {
            return _repository.Delete(t);
        }

        public Framework.InnerResult Update(Domains.UserEntity t)
        {
            return _repository.Update(t);
        }

        public IQueryable<Domains.UserEntity> Table
        {
            get { return _repository.Table; }
        }

        public IQueryable<Domains.UserEntity> TableNoTracking
        {
            get { return _repository.TableNoTracking; }
        }


    }
}
